package com.muyu.system.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.core.utils.bean.BeanUtils;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoAddresses;
import com.muyu.system.domain.*;
import com.muyu.system.req.CartItemDto;
import com.muyu.system.req.DanmoShoppingCartOrdersDto;
import com.muyu.system.req.DanmoShoppingOrdersReq;
import com.muyu.system.req.ShoppingCartDto;
import com.muyu.system.service.IDanmoShoppingCartService;
import com.muyu.system.service.IDanmoShoppingOrderDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 订单详情Controller
 *
 * @author yangle
 * @date 2024-09-29
 */
@RestController
@RequestMapping("/details")
public class DanmoShoppingOrderDetailsController extends BaseController {
    @Autowired
    private IDanmoShoppingOrderDetailsService danmoShoppingOrderDetailsService;

    @Autowired
    private  IDanmoShoppingCartService danmoShoppingCartService;


    /**
     * 查询订单详情列表
     */
//    @RequiresPermissions("system:details:list")
    @PostMapping("/list")
    public Result list(@RequestBody DanmoShoppingOrderDetails danmoShoppingOrderDetails) {
        PageHelper.startPage(danmoShoppingOrderDetails.getPageNum(), danmoShoppingOrderDetails.getPageSize());
        List<DanmoShoppingOrderDetails> list = danmoShoppingOrderDetailsService.selectDanmoShoppingOrderDetailsList(danmoShoppingOrderDetails);
        PageInfo<DanmoShoppingOrderDetails> danmoShoppingOrderDetailsPageInfo = new PageInfo<>(list);
        return Result.success(danmoShoppingOrderDetailsPageInfo);
    }

    /**
     * 导出订单详情列表
     */
//    @RequiresPermissions("system:details:export")
    @Log(title = "订单详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoShoppingOrderDetails danmoShoppingOrderDetails) {
        List<DanmoShoppingOrderDetails> list = danmoShoppingOrderDetailsService.selectDanmoShoppingOrderDetailsList(danmoShoppingOrderDetails);
        ExcelUtil<DanmoShoppingOrderDetails> util = new ExcelUtil<DanmoShoppingOrderDetails>(DanmoShoppingOrderDetails.class);
        util.exportExcel(response, list, "订单详情数据");
    }

    /**
     * 获取订单详情详细信息
     */
//    @RequiresPermissions("system:details:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return success(danmoShoppingOrderDetailsService.selectDanmoShoppingOrderDetailsById(id));
    }

    /**
     * 新增订单详情
     */
//    @RequiresPermissions("system:details:add")
    @Log(title = "订单详情", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DanmoShoppingOrderDetails danmoShoppingOrderDetails) {
        return toAjax(danmoShoppingOrderDetailsService.insertDanmoShoppingOrderDetails(danmoShoppingOrderDetails));
    }



    /**
     * 删除订单详情
     */
//    @RequiresPermissions("system:details:remove")
    @Log(title = "订单详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return toAjax(danmoShoppingOrderDetailsService.deleteDanmoShoppingOrderDetailsByIds(ids));
    }


    /**
     * 直接购买商品生成订单
     */
    @Log(title = "直接购买商品生成订单", businessType = BusinessType.UPDATE)
    @PostMapping("/purchaseGoods")
    public Result addSHoppingOrder(@RequestBody DanmoShoppingOrdersReq danmoShoppingOrdersReq) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoShoppingOrderDetailsService.addShoppingOrder(danmoShoppingOrdersReq);
    }

    /**
     * 通过购物车生成订单
     */
    @Log(title = "通过购物车生成订单", businessType = BusinessType.UPDATE)
    @PostMapping("/shoppingCart")
    public Result addCart() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        ShoppingCartDto shoppingCartDto =  danmoShoppingCartService.selectDanmoShoppingCartList(loginUser);
        Result result = danmoShoppingOrderDetailsService.selectDanmoShoppingCart(shoppingCartDto);
        Object data = result.getData();
        DanmoShoppingCartOrdersDto danmoShoppingCartOrdersDto = (DanmoShoppingCartOrdersDto) data; // 强制转换
        return danmoShoppingOrderDetailsService.addSHoppingOrderByShoppingCart(danmoShoppingCartOrdersDto);
    }


    /**
     * 购物车订单展示
     */
//    @RequiresPermissions("system:cart:edit")
    @Log(title = "购物车订单展示", businessType = BusinessType.UPDATE)
    @PutMapping("/shoppingCartOrder")
    public Result edit() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        ShoppingCartDto shoppingCartDto =  danmoShoppingCartService.selectDanmoShoppingCartList(loginUser);
        return danmoShoppingOrderDetailsService.selectDanmoShoppingCart(shoppingCartDto);
    }

    /**
     * 付款
     */
//    @RequiresPermissions("system:details:edit")
    @Log(title = "付款", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoShoppingOrdersReq danmoShoppingOrdersReq) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoShoppingOrderDetailsService.updateDanmoShoppingOrderDetails(danmoShoppingOrdersReq);
    }

    /**
     * 发货
     */
//    @RequiresPermissions("system:cart:edit")
    @Log(title = "发货", businessType = BusinessType.UPDATE)
    @PostMapping("/delivery")
    public Result delivery(@RequestBody DanmoShoppingOrdersReq danmoShoppingOrdersReq) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoShoppingOrderDetailsService.updateDanmoShoppingDelivery(danmoShoppingOrdersReq);
    }

    /**
     * 收货
     */
//    @RequiresPermissions("system:cart:edit")
    @Log(title = "收货", businessType = BusinessType.UPDATE)
    @PostMapping("/receiving")
    public Result receiving(@RequestBody DanmoShoppingOrdersReq danmoShoppingOrdersReq) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoShoppingOrderDetailsService.updateDanmoShoppingReceiving(danmoShoppingOrdersReq);
    }

    /**
     * 退货/售后
     */
//    @RequiresPermissions("system:cart:edit")
    @Log(title = "收获", businessType = BusinessType.UPDATE)
    @PostMapping("/afterSales")
    public Result afterSales(@RequestBody DanmoShoppingOrdersReq danmoShoppingOrdersReq) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoShoppingOrderDetailsService.updateDanmoShoppingAfterSales(danmoShoppingOrdersReq);
    }

    /**
     * 新增物流单号
     */
//    @RequiresPermissions("system:cart:edit")
    @Log(title = "新增物流单号", businessType = BusinessType.UPDATE)
    @PostMapping("/logisticsOrderNumber")
    public Result logisticsOrderNumber(@RequestBody DanmoShoppingOrdersReq danmoShoppingOrdersReq) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoShoppingOrderDetailsService.updateDanmoShoppingLogisticsOrderNumber(danmoShoppingOrdersReq);
    }
}
